// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_INV_CO
// * CreateDate    : 2013-12-20 23:31:42
// * Creater       : CodeSmith
// * LastChangeDate: 2013-12-20 23:31:42
// * LastChanger   : 2013-12-20 23:31:42
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Inv
{
/// <summary>
    /// InvCo
    /// </summary>
    public partial class InvCoBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objInvCoEntity">Entity</param>
        /// <returns>Entity</returns>
        public static InvCoEntity GetInvCo(InvCoEntity objInvCoEntity)
        {
            return DataAccess.SelectSingle<InvCoEntity>(objInvCoEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objInvCoEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertInvCo(InvCoEntity objInvCoEntity)
        {
            return DataAccess.Insert(objInvCoEntity);
        }

        public static bool InsertInvCo(List<InvCoEntity> objInvCoEntityList)
        {
            return DataAccess.Insert(objInvCoEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objInvCoEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateInvCo(InvCoEntity objInvCoEntity)
        {
            return DataAccess.Update(objInvCoEntity);
        }

        public static bool UpdateInvCo(List<InvCoEntity> objInvCoEntityList)
        {
            return DataAccess.Update<InvCoEntity>(objInvCoEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objInvCoEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteInvCo(InvCoEntity objInvCoEntity)
        {
            return DataAccess.Delete(objInvCoEntity);
        }

        public static bool DeleteInvCo(List<InvCoEntity> objInvCoEntityList)
        {
            return DataAccess.Delete<InvCoEntity>(objInvCoEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetInvCoAll()
        {
            return DataAccess.SelectAllDataSet<InvCoEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<InvCoEntity> GetInvCoAllList()
        {
            return DataAccess.SelectAll<InvCoEntity>();
        }
        #endregion

        public static bool InsertInvCo(InvCoEntity co,List<InvCoiEntity> coi)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Insert(co, broker);
                    DataAccess.Insert(coi, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static DataSet GetAllDsByCondition(int pageIndex, int pageCount, string condition)
        {
            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from INV_CO) as cnt
from INV_CO
where id not in (
    select top ({1}*({2}-1)) id from INV_CO order by created_date
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageCount, pageCount, pageIndex);
                sql = @"select co.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from ({0}) co
left join BPMDB.dbo.PW_User u1 on co.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on co.UPDATED_BY=u2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date
) order by created_date desc";
                string sqlSource = @"select co.*,u1.firstName as CREATED_USER,u2.firstName as UPDATED_USER 
from INV_CO co
left join BPMDB.dbo.PW_User u1 on co.CREATED_BY=u1.userNo
left join BPMDB.dbo.PW_User u2 on co.UPDATED_BY=u2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageCount, pageCount, pageIndex, sqlSource);
            }
            return DataAccess.SelectDataSet(sql);
        }

	}
	
	
}	

